package indi.qing.controller;

import com.auth0.jwt.JWT;
import indi.qing.VO.UserVO;
import indi.qing.VO.LoginVO;
import indi.qing.VO.UserLoginVO;
import indi.qing.common.JWTUtils;
import indi.qing.common.Result;
import indi.qing.entity.Device;
import indi.qing.entity.User;
import indi.qing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 熊卿智
 * @since 2021-12-12
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user/register")
    public Result register(@RequestBody User user){
        try {
            int succ = userService.register(user);
            if(succ == 0) {
                return Result.failed("注册失败");
            }
            return Result.ok("注册成功");
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/login")
    public Result login(@RequestBody LoginVO loginVO){
        try {
            UserLoginVO userLoginVO = userService.login(loginVO.getPhone(),loginVO.getPassword(),loginVO.getRole());
            Map<String,String> payload = new HashMap<>();
            payload.put("userId", String.valueOf(userLoginVO.getUserId()));
            payload.put("username", userLoginVO.getUsername());
            payload.put("role", String.valueOf(loginVO.getRole()));
            //生成JWT的令牌
            String token = JWTUtils.getToken(payload);
            userLoginVO.setToken(token);
            return Result.ok(userLoginVO);
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/bandDevice")
    public Result bandDevice(HttpServletRequest request, @RequestBody Device device){
        String token = request.getHeader("token");
        Long userId = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        try {
            userService.bandDevice(userId,device.getDeviceSn());
            return Result.ok("绑定成功");
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/findFriend")
    public Result findFriend(HttpServletRequest request,@RequestBody User user){
        String token = request.getHeader("token");
        Long userId = Long.valueOf(JWT.decode(token).getClaim("userId").asString());
        try {
            List<UserVO> usersVO = userService.findFriend(userId,user.getPhone());
            return Result.ok(usersVO);
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/user/findUserById/{userId}")
    public Result findUserById( @PathVariable Long userId){
        try {
            UserVO usersVO = userService.findUserById(userId);
            return Result.ok(usersVO);
        }catch (Exception e){
            return Result.failed(e.getMessage());
        }
    }



}

